package osb.map.main;

import osb.map.net.ClientSocket;
import osb.map.publicdata.Public_Operate_And_Data;
import com.ljp.ani.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * 引导界面 - 第一次启动
 * @author Administrator
 *
 */

public class Welcome_Firsttime_In_main extends Activity implements Welcome_Firsttime_In_OnViewChangeListener
{
    private Welcome_Firsttime_In_ScrollLayout mScrollLayout;
    private ImageView[] imgs;
    private int count;
    private int currentItem;
    private Button startBtn;
    private RelativeLayout mainRLayout;
    private LinearLayout pointLLayout;
    private LinearLayout leftLayout;
    private LinearLayout rightLayout;
    private LinearLayout animLayout;

    public static Thread thread;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
       
        // 判断程序是否第一次运行 
        SharedPreferences preferences = getSharedPreferences("coun1t", MODE_WORLD_READABLE);
        int count = preferences.getInt("coun1t", 0);

        thread = new Thread(new ClientSocket()); // 服务器
        thread.start();
        
        //  第一次
        if(Public_Operate_And_Data.Showguide) 
        {
            setContentView(R.layout.welcome_firsttime_access);
            initView();
            return;
        }

        // 第一次运行
        if ((count == 0) && !Public_Operate_And_Data.Showguide) 
        {
            
            setContentView(R.layout.welcome_firsttime_access);
            initView();
            Editor editor = preferences.edit();
            editor.putInt("coun1t", ++count);
            editor.commit();
        }
        else
        {
            //非第一次运行
            Intent intent = new Intent();
            intent.setClass(Welcome_Firsttime_In_main.this, Welcome.class); //不启动引导画面    直接显示启动画面
            startActivity(intent);
            Welcome_Firsttime_In_main.this.finish();
        }

    }

    /**
     * 初始化第一次运行引导界面
     */
    private void initView() 
    {
        mScrollLayout = (Welcome_Firsttime_In_ScrollLayout) findViewById(R.id.ScrollLayout);
        pointLLayout = (LinearLayout) findViewById(R.id.llayout);
        mainRLayout = (RelativeLayout) findViewById(R.id.mainRLayout);
        startBtn = (Button) findViewById(R.id.startBtn);
        startBtn.setOnClickListener(onClick);
        animLayout = (LinearLayout) findViewById(R.id.animLayout);
        leftLayout = (LinearLayout) findViewById(R.id.leftLayout);
        rightLayout = (LinearLayout) findViewById(R.id.rightLayout);
        count = mScrollLayout.getChildCount();
        imgs = new ImageView[count];
        for (int i = 0; i < count; i++)
        {
            imgs[i] = (ImageView) pointLLayout.getChildAt(i);
            imgs[i].setEnabled(true);
            imgs[i].setTag(i);
        }
        currentItem = 0;
        imgs[currentItem].setEnabled(false);
        mScrollLayout.SetOnViewChangeListener(this);
    }

    /**
     * 界面点击监听
     */
    private View.OnClickListener onClick = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
            // 引导界面结束 - 进入主界面
            case R.id.startBtn:
                mScrollLayout.setVisibility(View.GONE);
                pointLLayout.setVisibility(View.GONE);
                animLayout.setVisibility(View.VISIBLE);
                mainRLayout.setBackgroundResource(R.drawable.whatsnew_bg);
                Animation leftOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.xml.translate_left);
                Animation rightOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.xml.translate_right);
                // Animation leftOutAnimation =
                // AnimationUtils.loadAnimation(getApplicationContext(),
                // R.anim.fadedout_to_left_down);
                // Animation rightOutAnimation =
                // AnimationUtils.loadAnimation(getApplicationContext(),
                // R.anim.fadedout_to_right_down);
                leftLayout.setAnimation(leftOutAnimation);
                rightLayout.setAnimation(rightOutAnimation);
                leftOutAnimation.setAnimationListener(new AnimationListener()
                {
                    @Override
                    public void onAnimationStart(Animation animation)
                    {
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation)
                    {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation)
                    {
                        leftLayout.setVisibility(View.GONE);
                        rightLayout.setVisibility(View.GONE);
                        Intent intent = new Intent(Welcome_Firsttime_In_main.this,Main_Activity.class);    //最后跳转到主界面
                        Public_Operate_And_Data.Showguide = false;
                        Welcome_Firsttime_In_main.this.startActivity(intent);
                        Welcome_Firsttime_In_main.this.finish();
                        overridePendingTransition(R.xml.zoom_out_enter,  R.xml.zoom_out_exit);
                    }
                });
                break;
            }
        }
    };

    @Override
    public void OnViewChange(int position)
    {
        setcurrentPoint(position);
    }

    private void setcurrentPoint(int position)
    {
        if (position < 0 || position > count - 1 || currentItem == position)
        {
            return;
        }
        imgs[currentItem].setEnabled(true);
        imgs[position].setEnabled(false);
        currentItem = position;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            if (Public_Operate_And_Data.Showguide)  
            {
                Intent intent = new Intent();
                intent.setClass(Welcome_Firsttime_In_main.this, Main_Activity.class);
                startActivity(intent);
                Welcome_Firsttime_In_main.this.finish();
            }
            else      
            {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}